using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using WareForms;
using System.IO;
using System.Diagnostics;

namespace RSSApplicationUpdater
{
    public partial class ProgressDialog : WareForm
    {
        public ApplicationUpdater core = new ApplicationUpdater(true);
        bool execcommands = false;
        string tfilename, currentfileDownload;
        public ProgressDialog()
        {
            InitializeComponent();
        }
        public ProgressDialog(bool exec,string filename)
        {
            InitializeComponent();
            execcommands = exec;
            tfilename = filename;
            
        }
        public ProgressDialog(bool exec)
        {
            InitializeComponent();
            execcommands = exec;
           // tfilename = filename;

        }

        private void ProgressDialog_Load(object sender, EventArgs e)
        {
            //string url = core.CheckforNewVersion();

            if (execcommands == true)
            {
                string url = core.CheckforNewVersion();
                string tfilename;

                if (url != null)
                {
                    tfilename = Path.GetFileName(url);

                    if (Directory.Exists(Application.StartupPath + "\\" + ApplicationUpdater.updatesfolder) == false)
                    {
                        Directory.CreateDirectory(Application.StartupPath + "\\" + ApplicationUpdater.updatesfolder);

                    }
                    if (tfilename != null)
                    {
                        if (File.Exists(Application.StartupPath + "\\" + ApplicationUpdater.updatesfolder + "\\" + tfilename) == true)
                        {
                            File.Delete(Application.StartupPath + "\\" + ApplicationUpdater.updatesfolder + "\\" + tfilename);

                        }
                        ApplicationUpdater.clent = new WebClient();
                        ApplicationUpdater.clent.DownloadProgressChanged += new DownloadProgressChangedEventHandler(Webclient_Progresschanged);
                        ApplicationUpdater.clent.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(this.Webclient_FileCompleted);
                        this.Text = "RSS  Application Updater";
                        Uri url2 = new Uri(url);
                        ApplicationUpdater.clent.DownloadFileAsync(url2, Application.StartupPath + "\\" + Core.updatesfolder + "\\" + tfilename);

                        ApplicationUpdater.clent.DownloadDataCompleted += new DownloadDataCompletedEventHandler(this.webclient_DownloadDataCompleted);

                        currentfileDownload = Application.StartupPath + "\\" + Core.updatesfolder + "\\" + tfilename;
                    }
                }
                else
                {
                    MessageBox.Show("No Update is available");

                }
            }
           
        }
        void webclient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs ev)
        {
            try
            {
                this.Close();
            }

            catch (Exception e)
            {

                Core.errorreport(e);
            }


        }
        private void Webclient_Progresschanged(object sender, DownloadProgressChangedEventArgs ev)
        {
            try
            {
                this.Text = "RSS Application Updater " + ev.ProgressPercentage + " %";
                this.progressBar.Value = ev.ProgressPercentage;
                //this.DefaultProgressBar.Refresh() ;
               

            }

            catch (Exception e)
            {

                Core.errorreport(e);
            }
        }
        private void Webclient_FileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs ev)
        {
            try
            {
                if ((currentfileDownload != null) || (File.Exists(currentfileDownload) == true))
                {
                    Process.Start(currentfileDownload);
                    MessageBox.Show("Download Completed \nNow The Application will close and the update will be installed.");
                    System.Windows.Forms.Application.Exit();
                }
               
                this.Close();
                
                    
               
                
            }

            catch (Exception e)
            {

                Core.errorreport(e);
            }

        }

        private void tmrValueRefresh_Tick(object sender, EventArgs ev)
        {
            try
            { 
            }

            catch (Exception e)
            {

                Core.errorreport(e);
            }

        }

    }
}